package com.itzjc.exception;

import com.itzjc.domain.entity.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理类
 *
 * @className: CustomExceptionHandler
 * @date: 2021-06-04 14:51
 **/
@ControllerAdvice
public class CustomExceptionHandler {
    //加入日志
    private final static Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        logger.error("【系统异常】{}", e);
        if (e instanceof MyException) {
            MyException myException = (MyException) e;
            return Result.buildError(myException.getMessage(), myException.getCode());
        } else {
            return Result.buildError("全局异常，未知错误", 300);
        }
    }
}
